import {Progress, Divider, Space, Statistic, Typography} from 'antd';
import {getTimeDifference} from '../utils/convert';
import React from 'react';
import {
  ApiOutlined,
  BugOutlined,
  FileDoneOutlined,
  FileExclamationOutlined,
  GlobalOutlined,
  HourglassOutlined,
  MinusCircleOutlined,
  MinusOutlined,
  PoweroffOutlined,
} from '@ant-design/icons';

const {Text}=Typography;


const getReasonDisplay=(reason)=>{
  if (!reason) return null;
  const props={
    style: {
      margin: '-32px 0 0 0',
    },
  };
  switch (reason) {
    case 'manual':
      return (
        <Text type={'warning'}
          {...props}>
          <Space>
            <MinusCircleOutlined />
            {'已被用户手动取消，可以重新索引新建相同任务'}
          </Space>
        </Text>);
    case 'worker terminated':
      return (
        <Text type={'danger'}
          {...props}>
          <Space>
            <PoweroffOutlined />
            {'队列执行进程意外关闭，可能由于服务器宕机'}
          </Space>
        </Text>);
    case 'spider crashed':
      return (
        <Text type={'danger'}
          {...props}>
          <Space>
            <ApiOutlined />
            {'爬虫意外退出，请检查输出'}
          </Space>
        </Text>);
    case 'job exception':
      return (
        <Text type={'danger'}
          {...props}>
          <Space>
            <BugOutlined />
            {'队列任务执行错误，请检查输出或服务器错误记录'}
          </Space>
        </Text>);
    case 'complete':
      return (
        <Text type={'success'}
          {...props}>
          <Space>
            <FileDoneOutlined />
            {'全部爬取完毕'}
          </Space>
        </Text>);
    case 'max page':
      return (
        <Text type={'success'}
          {...props}>
          <Space>
            <FileDoneOutlined />
            {'爬取达到指定数目'}
          </Space>
        </Text>);
    case 'parse error':
      return (
        <Text type={'warning'}
          {...props}>
          <Space>
            <FileExclamationOutlined />
            {'目录页面解析失败，可能由于 IP、代理被封禁或网络错误'}
          </Space>
        </Text>);
    case 'proxy error':
      return (
        <Text type={'warning'}
          {...props}>
          <Space>
            <GlobalOutlined />
            {'代理获取失败，可能是代理池服务未启动或过期'}
          </Space>
        </Text>);
    default:
      return (
        <Text type={'warning'}
          {...props}>
          <Space>
            <BugOutlined /> {'未知错误'}
          </Space>
        </Text>);
  }
};


const getProgressBar = (status) => {
  const props = {
    type: 'circle',
    width: 60,
  };
  if (status.status === 'finished') {
    return <Progress {...props} percent={100}/>;
  }
  if (status.status === 'failed') {
    return <Progress {...props} percent={100} status={'exception'}/>;
  }
  if (status.status === 'queuing') {
    return <Progress
      {...props}
      strokeColor={'#9950ff'}
      percent={100}
      format={()=><Text style={{color: '#9950ff'}}>
        <HourglassOutlined spin/>
      </Text>}
    />;
  }
  if (status.status === 'running') {
    return <Progress
      {...props}
      percent={Number(status.percent).toFixed()}
      status={'active'}
    />;
  }
  if (status.status === 'cancelling' ||
    status.status === 'cancelled') {
    return <Progress
      {...props}
      strokeColor={'#faad14'}
      percent={100}
      format={()=><Text type={'warning'}><MinusOutlined/></Text>}
    />;
  }
};


export const SpiderInfo=({parsedSpider: spider, nowDate})=>{
  const noWrap={
    whiteSpace: 'no-wrap',
  };
  return (
    <Space size={'middle'} style={{width: '100%'}} direction={'vertical'}>
      {getReasonDisplay(spider.reason)}
      <Space size={'large'}>
        {getProgressBar(spider)}
        <Divider type={'vertical'}/>
        <Statistic style={noWrap}
          title="爬取"
          value={
            spider.type==='jd'?
              spider.jd_books_count:
                (spider.type==='dd'?
                  spider.dd_books_count:
                    spider.progress)
          }
          suffix={Number(spider.max_count)?`/${spider.max_count}`:(spider.type==='comment'?'商品':'条')}
        />
        <Divider type={'vertical'}/>
        <Statistic style={noWrap} title={spider.type==='comment'?'ISBN':'关键词'} formatter={(v)=>v} value={String(spider.key)}/>
        {spider.status==='running'&&<><Divider type={'vertical'}/>
          <Statistic style={noWrap} title="已运行"
            value={getTimeDifference(spider.originalStartedAt, nowDate)}/>
        </>}
        {
          spider.originalStartedAt&&
          spider.originalFinishedAt&&
          <>
            <Divider type={'vertical'}/>
            <Statistic style={noWrap} title="耗时"
              value={
                getTimeDifference(spider.originalStartedAt,
                    spider.originalFinishedAt,
                )
              }
            />
          </>
        }
      </Space>
    </Space>
  );
};
